package com.grm.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grm.common.entity.Result;
import com.grm.common.annotation.HasPerm;
import com.grm.common.annotation.Log;
import com.grm.sys.model.SysDictData;
import com.grm.sys.request.dict.data.SysDictDataAddParam;
import com.grm.sys.request.dict.data.SysDictDataQueryParam;
import com.grm.sys.request.dict.data.SysDictDataUpdateParam;
import com.grm.sys.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/dict/data")
public class SysDictDataController {
    @Autowired
    private SysDictDataService sysDictDataService;

    @GetMapping("/selected/{type}")
    public Result querySelectByType(@PathVariable("type") String type) {
        List<SysDictData> list = sysDictDataService.querySelectByType(type);
        return Result.success(list);
    }

    @PostMapping("/list")
    public Result querySelective(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @RequestParam(required = false, defaultValue = "30") Integer pageSize,
        @RequestBody SysDictDataQueryParam queryParam) {
        // 开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<SysDictData> sysDictDatas = sysDictDataService.querySelective(queryParam);
        PageInfo<SysDictData> pageInfo = new PageInfo<>(sysDictDatas);
        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    @Log("删除字典数据")
    @PostMapping("/delete")
    @HasPerm("sys:dict:data:delete")
    public Result delete(@RequestBody List<Long> ids) {
        sysDictDataService.delete(ids);
        return Result.success();
    }

    @Log("添加字典数据")
    @PostMapping("/add")
    @HasPerm("sys:dict:data:add")
    public Result add(@RequestBody SysDictDataAddParam addParam) {
        sysDictDataService.add(addParam);
        return Result.success();
    }

    @Log("修改字典数据")
    @PutMapping("/update")
    @HasPerm("sys:dict:data:update")
    public Result update(@RequestBody SysDictDataUpdateParam updateParam) {
        sysDictDataService.update(updateParam);
        return Result.success();
    }
}
